/**
 * @file axios.js
 * @description 封装axios请求
 * @author stone
 * @date 2024-03-11
 */

import axios from 'axios'
import { BASE_URL, URL } from './URL'
import { ElMessage } from 'element-plus'
import router from '../router'
// 创建axios实例
const instance = axios.create({
    baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PROD_URL : "/api"}`,
    timeout: 1000 * 5,
    withCredentials: true,
    headers: {
        'Content-Type': "application/x-www-form-urlencoded"
    }
})

//响应拦截器
instance.interceptors.response.use(res => {
    if (res.data.code === 401) {
        ElMessage.error(res.data.msg)
        router.push('/Login').then(() => {
            console.log('ok');
        }).catch(err => {
            console.log(err);
        })
        return Promise.reject(res)
    }
    return res
}
    , err => {
        return Promise.reject(err)
    }
)

export const post = (url, data) => {
    return instance.post(url, data)
}

export const get = (url, data) => {
    return new Promise((resolve, reject) => {
        instance.get(url, data).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}


